var longitude = $("#areaLongitude").val();
var latitude = $("#areaLatitude").val();
if(longitude == null || longitude == "" || longitude == undefined || latitude == null || latitude == "" || latitude == undefined){
    longitude = 121.480237;
    latitude = 31.236305;
}

// 百度地图获取中心坐标点
var map2 = new BMap.Map('allmap2',{enableMapClick:false});
var poi2 = new BMap.Point(longitude,latitude);
map2.centerAndZoom(poi2, 14); //16个级别 18最高 最低3
map2.enableScrollWheelZoom();

var marker2 = new BMap.Marker(poi2);  // 创建标注
map2.addOverlay(marker2);               // 将标注添加到地图中
marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
//单击获取点击的经纬度
map2.addEventListener("click", showInfo);
function showInfo(e){
    $("#areaLongitude").val(e.point.lng);
    $("#areaLatitude").val(e.point.lat);
    map2.clearOverlays();
    poi2=new BMap.Point(e.point.lng,e.point.lat);
    marker2 = new BMap.Marker(poi2);  // 创建标注
    map2.addOverlay(marker2);               // 将标注添加到地图中
    marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
}


// 百度地图描边功能
var map = new BMap.Map('map',{enableMapClick:false});
var poi = new BMap.Point(longitude,latitude);
map.centerAndZoom(poi, 17);
map.enableScrollWheelZoom();
loadPath($("#areaPath").val()); // 在地图上加载描边


function getMapMessage(uid) {
    $.ajax({
        type: "GET",
        url: "/map/getMapMessage",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            uid:uid
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                geoToPoints(data.data.geo);
            } else {
                alert(data.message);
            }
        }
    });
}

function getMapAddress(uid) {
    $.ajax({
        type: "GET",
        url: "/map/getAddressMsg",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            uid:uid
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                $("#address").val(data.data.address);
                var projection = BMAP_NORMAL_MAP.getProjection();
                var addrPoint = projection.pointToLngLat(new BMap.Pixel(data.data.addrLongitude, data.data.addrLatitude));
                $("#addrLongitude").val([addrPoint.lng].toString());
                $("#addrLatitude").val([addrPoint.lat].toString());

                var passageway="";
                var geoStr="";
                var list = data.data.listView;
                $(list).each(function(){
                    passageway += this.address + ",";
                    geoStr += this.longitude + "," + this.latitude + ";";
                });
                if(passageway.indexOf(",") >= 0){
                    $("#passageway").val(passageway.substring(0,passageway.length-1));
                }
                if(geoStr.indexOf(";") >= 0){
                    var geo = geoStr.substring(0,geoStr.length-1)
                    var points ="";
                    if (geo) {
                        var lngs ="";
                        var lats ="";
                        var projection = BMAP_NORMAL_MAP.getProjection();
                        if (geo && geo.indexOf(";") >= 0) {
                            geo = geo.split(';');
                        }
                        for ( var i = 0, len = geo.length; i < len; i++) {
                            var pos = geo[i].split(',');
                            var point = projection.pointToLngLat(new BMap.Pixel(pos[0], pos[1]));
                            lngs += ([point.lng].toString() + ",");
                            lats += ([point.lat].toString() + ",");
                        }
                    }
                    if(lngs.indexOf(",") >= 0){
                        $("#pwLongitude").val(lngs.substring(0,lngs.length-1));
                    }
                    if(lats.indexOf(",") >= 0){
                        $("#pwLatitude").val(lats.substring(0,lats.length-1));
                    }
                }
            } else {
                alert(data.message);
            }
        }
    });
}


// 米制坐标转换成百度经纬度坐标并描边
function geoToPoints(geo) {
    var points ="";
    if (geo) {
        var projection = BMAP_NORMAL_MAP.getProjection();
        if (geo && geo.indexOf(";") >= 0) {
            geo = geo.split(';');
        }
        for ( var i = 0, len = geo.length; i < len; i++) {
            var pos = geo[i].split(',');
            var point = projection.pointToLngLat(new BMap.Pixel(pos[0], pos[1]));
            points += ([ point.lng, point.lat ].toString() + "_");
        }
    }
    if(points != null || points != "") {
        points = points.substr(0, points.length - 1);
        $("#areaPath").val(points);  //检索结果内嵌到页面上
        loadPath(points); //在地图上加载描边
    }
}

//在地图上加载描边
function loadPath(points) {
    var polygonPointsArray = new Array(	);
    var pathStr = points.split("_");
    for (var i=0;i < pathStr.length;i++) {
        var p = pathStr[i].split(',');
        polygonPointsArray[i] = new BMap.Point(p[0],p[1]);
    }
    polygon = new BMap.Polygon(polygonPointsArray,{
        strokeColor:"#EE7942",    //边线颜色。
        fillColor:"#EE7942",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 2,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
        fillOpacity: 0.4,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'dashed' //边线的样式，solid或dashed。
    });  //创建多边形
    map.clearOverlays();// 清空地图上标注点
    map.addOverlay(polygon);   //增加多边形
}

//在地图上绘制描边
var overlays = [];
var pathStr = "";
var overlaycomplete = function(e){
    overlays.push(e.overlay);
    var path = e.overlay.getPath();
    for(var i=0;i<path.length;i++){
        pathStr += path[i].lng + "," + path[i].lat + "_";
    }
    $("#areaPath").val(pathStr.substr(0,pathStr.length-1));
};
var styleOptions = {
    strokeColor:"#EE7942",    //边线颜色。
    fillColor:"#EE7942",      //填充颜色。当参数为空时，圆形将没有填充效果。
    strokeWeight: 2,       //边线的宽度，以像素为单位。
    strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
    fillOpacity: 0.4,      //填充的透明度，取值范围0 - 1。
    strokeStyle: 'dashed' //边线的样式，solid或dashed。
}
//实例化鼠标绘制工具
var drawingManager = new BMapLib.DrawingManager(map, {
    isOpen: false, //是否开启绘制模式
    enableDrawingTool: true, //是否显示工具栏
    drawingMode:BMAP_DRAWING_POLYGON ,//画线
    drawingToolOptions: {
        anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
        offset: new BMap.Size(5, 5), //偏离值
        drawingModes:[
            BMAP_DRAWING_POLYGON
        ]
    },
    polygonOptions: styleOptions //多边形的样式
});

//添加鼠标绘制工具监听事件，用于获取绘制结果
drawingManager.addEventListener('overlaycomplete', overlaycomplete);

// 清除绘制的描边
function clearAll() {
    for(var i = 0; i < overlays.length; i++){
        map.removeOverlay(overlays[i]);
    }
    overlays.length = 0
    map.removeOverlay(polygon);
    $("#areaPath").val(null);
}

//定位中心点
function theLocation(cityName){
    if(cityName != ""){
        map.centerAndZoom(cityName,20);
        map2.centerAndZoom(cityName, 20);
    }
}
